// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MockTransport.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Test.Robotics.Hardware.DAC
{
    using System;

    using Microsoft.Robotics.Hardware.DAC;

    /// <summary>
    /// Implements the ITransport interface 
    /// </summary>
    public class MockTransport : ITransport
    {
        /// <summary>
        /// The buffer of data used for AsyncReads
        /// </summary>
        private byte[] buffer;

        /// <summary>
        /// Initializes a new instance of the <see cref="MockTransport" /> class.
        /// </summary>
        /// <param name="buffer">A buffer containing the data that will provided on a completed AsyncRead</param>
        public MockTransport(byte[] buffer)
        {
            this.buffer = buffer;
        }

        /// <summary>
        /// Activates the MockEventArgs 
        /// </summary>
        /// <param name="eventArgs">The MockEventArgs that will use used</param>
        /// <returns>True when a MockEventArgs is received, false otherwise</returns>
        public bool AsyncRead(AsyncEventArgs eventArgs)
        {
            if (!(eventArgs is MockEventArgs))
            {
                return false;
            }

            ((MockEventArgs)eventArgs).On();
            return true;
        }

        /// <summary>
        /// Unused Method
        /// </summary>
        /// <param name="eventArgs">The parameter is not used.</param>
        /// <returns>True in all cases</returns>
        public bool AsyncWrite(AsyncEventArgs eventArgs)
        {
            return true;
        }

        /// <summary>
        /// Unused Method
        /// </summary>
        public void Connect()
        {
        }

        /// <summary>
        /// Unused Method
        /// </summary>
        public void Disconnect()
        {
        }

        /// <summary>
        /// Provides a new MockEventArgs object
        /// </summary>
        /// <param name="ignoredBuffer">The parameter is not used.</param>
        /// <returns>A MockEventArgs object</returns>
        public AsyncEventArgs PrepareAsyncOperation(byte[] ignoredBuffer)
        {
            return new MockEventArgs(this, (byte[])this.buffer.Clone());
        }

        /// <summary>
        /// Implements the dispose method
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Disposes of the class instance
        /// </summary>
        /// <param name="disposing">True if disposing, false if finalizing</param>
        private void Dispose(bool disposing)
        {
        }
    }
}
